<?php
/*
	info: VIP品牌
	date: 2011-12-19
*/

if (!defined('ADMIN_WEBINROOT')) {
	header("HTTP/1.0 404 Not Found");
	exit();
}

require WEBFILEPATH.'/include/category.class.php';
$areaDataModel = CategoryDataModel::factory('area', array('cache' => true));
$webglobal['file_brand'] = array();
$webglobal['file_brand']['citydata'] = $areaDataModel->getCategoryForLevel(1);

$webglobal['file_brand']['imagepath'] = '/upfiles/brand/'.global_FormatTime(0, 'Ym');
$webglobal['file_brand']['imagesetting'] = array(
	'type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
	'size'=>2097152
);

if ($webglobal['mode'] == 'brand'){
	/*get area_id=>area_name list*/
	$webglobal['file_brand']['areaArray'] = array();
	foreach($webglobal['file_brand']['citydata'] as $key =>$cityArray){
		$webglobal['file_brand']['areaArray'][$cityArray['id']] = $cityArray['name'];
	}
	/* url settings*/
	$webglobal['file_brand']['searchurl'] = '';
	$webglobal['file_brand']['gourlencode'] = '';
	$webglobal['file_brand']['gourl'] = "page={$webrequest['page']}";
	/*order $webrequest[order]*/
	$temporderstr = isset($webrequest['order'])?$webrequest['order']:"id";
	$webrequest['status'] = isset($webrequest['status'])?$webrequest['status']:999;
	
	if ($webrequest['status'] != 999) {
		$tempsearchsql[] = "status='{$webrequest['status']}'";
		$webglobal['file_brand']['gourl'] .= "&status={$webrequest['status']}";
		$webglobal['file_brand']['searchurl'] .= "&status={$webrequest['status']}";
	}
	
	$webrequest['searchkey'] = isset($webrequest['searchkey'])?$webrequest['searchkey']:'';
	if ($webrequest['searchkey'] != '') {
		$tempsearchsql[] = $db->BuildSearchSql($webrequest['searchkey'], array('name','description'));
		$webglobal['file_brand']['gourl'] .= "&searchkey=".urlencode($webrequest['searchkey']);
		$webglobal['file_brand']['searchurl'] .= "&searchkey=".urlencode($webrequest['searchkey']);
	}
	$tempsearchsql = empty($tempsearchsql) ? '' : ' WHERE '.implode(' AND ', $tempsearchsql);
	$webglobal['file_brand']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}brand_info{$tempsearchsql}");

	if ($webglobal['file_brand']['datanum'] > 0) {
		$webglobal['file_brand']['perpage'] = 10;
		$webglobal['page'] = global_Page($webglobal['file_brand']['datanum'], $webglobal['file_brand']['perpage'], $webrequest['page'], ADMIN_WEBURLPATH."/index.php?mode=brand&page=@@");
		
		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_brand']['perpage'];
		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}brand_info{$tempsearchsql} ORDER BY {$temporderstr} LIMIT $tempstartnum, {$webglobal['file_brand']['perpage']}");
		
		$temptr = 1;
		$webglobal['file_brand']['dataarray'] = array();
		$tempCompanyId = array();
		
		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
			$tempdata['addtime_show'] = !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
			$webglobal['file_brand']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
} else if ($webglobal['mode'] == 'brandadd'){
	if (global_CheckSubmit('adminsubmit')) {
			include WEBFILEPATH.'/include/upload.class.php';
			
			$tempmoreshowinfo = $tempimageurl = '';
			$tempimageupload = new Upload('image', $webglobal['file_brand']['imagesetting']);
			if ($tempimageupload->CheckFile()) {
				$tempimageupload->SaveFile(WEBFILEPATH.$webglobal['file_brand']['imagepath']);
				$tempimageurl = $webglobal['file_brand']['imagepath'].'/'.$tempimageupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/' . $tempimageupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();

				//Big Image:205 133
				$bigImage = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/big_' . $tempimageupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/small_' . $tempimageupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 225 , 133 , $bigImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 48 , 48 , $smallImage, 5, FALSE);
			} elseif (!$tempimageupload->isNone) {
				$tempmoreshowinfo .= '<br /><span class="dark">上传的图片文件不符合条件已忽略。</span>';
			}

			if ($tempimageurl!='') {
				$webglobal['CPD']['logo'] = $tempimageurl;
			}
			$webglobal['CPD']['name'] = $webrequest['name'];
			$webglobal['CPD']['area_id'] = $webrequest['area_id'];
			$webglobal['CPD']['description'] = $webrequest['description'];
			$webglobal['CPD']['status'] = $webrequest['status'];
			

			$tempnewsid = $db->Insert('brand_info', '', TRUE);
			admin_AddLog("添加VIP品牌|id: {$tempnewsid}; title: {$webrequest['name']}");
			admin_ShowMessage('VIP品牌添加成功。', ADMIN_WEBURLPATH."/index.php?mode=brand");
		} else{
			admin_StartFromForm();
		}
	
}else if ($webglobal['mode'] == 'brandedit'){
	$webrequest['editid'] = isset($webrequest['editid']) ? intval($webrequest['editid']) : 0;
	if ($webrequest['editid'] <= 0) {
		admin_ShowMessage('不存在该品牌。', '', 'BACK');
	}
	$webglobal['file_brand']['editinfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}brand_info WHERE id='{$webrequest['editid']}'");
	if (!$webglobal['file_brand']['editinfo']) {
		admin_ShowMessage('不存在该品牌。', '', 'BACK');
	}
	if (global_CheckSubmit('adminsubmit'))
	{
		include WEBFILEPATH.'/include/upload.class.php';
	
		if (!empty($webglobal['CPM'])) {
			admin_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {

			include WEBFILEPATH.'/include/upload.class.php';

			$tempmoreshowinfo = $tempimageurl = '';
			$tempimageupload = new Upload('image', $webglobal['file_brand']['imagesetting']);
			if ($tempimageupload->CheckFile()) {
				$tempimageupload->SaveFile(WEBFILEPATH.$webglobal['file_brand']['imagepath']);
				$tempimageurl = $webglobal['file_brand']['imagepath'].'/'.$tempimageupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/' . $tempimageupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();

				//Big Image:205 133
				$bigImage = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/big_' . $tempimageupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_brand']['imagepath'] . '/small_' . $tempimageupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 225 , 133 , $bigImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 48 , 48 , $smallImage, 5, FALSE);

			} elseif (!$tempimageupload->isNone) {
				$tempmoreshowinfo .= '<br /><span class="dark">上传的logo文件不符合条件已忽略。</span>';
			}
			$webrequest['image_del'] = isset($webrequest['image_del'])?$webrequest['image_del']:0;
			if ($webrequest['image_del']>0 || $tempimageurl!='')
			{
				if ($webglobal['file_brand']['editinfo']['logo'] != '')
				{
					@unlink(WEBFILEPATH.$webglobal['file_brand']['editinfo']['logo']);
				}
				$webglobal['CPD']['logo'] = $tempimageurl;
			}
			$webglobal['CPD']['name'] = $webrequest['name'];
			$webglobal['CPD']['area_id'] = $webrequest['area_id'];
			$webglobal['CPD']['description'] = $webrequest['description'];
			$webglobal['CPD']['status'] = $webrequest['status'];
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=brand&{$webrequest['gourl']}");

			$db->Update('brand_info', '', "id='{$webrequest['editid']}'");

			admin_AddLog("修改VIP品牌|id: {$webrequest['editid']}");
			admin_ShowMessage("VIP品牌修改成功。{$tempmoreshowinfo}", ADMIN_WEBURLPATH."/index.php?mode=brand&{$webrequest['gourl']}");
		}
	} else {
		$webglobal['file_brand']['gourlencode'] = urlencode($webrequest['gourl']);
		$webglobal['file_brand']['editinfo']['addtime_show'] = !empty($webglobal['file_brand']['editinfo']['addtime']) ? global_FormatTime($webglobal['file_brand']['editinfo']['addtime']) : '';
		admin_StartFromForm();
	}

} elseif ($webglobal['mode']=='brandup') {
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}

	$tempauditstr = '';
	if ($webrequest['access'] == 'update') {
		if (!empty($webrequest['delid'])) {
			$tempauditstr = implode(',', $webrequest['delid']);
			$db->Update('brand_info', "status='1'", 'id IN ('.$db->BuildInSql($webrequest['delid']).')');
		}
	}

	if ($webrequest['access'] == 'delete') {
		if (!empty($webrequest['delid'])) {
			$tempdelstr = implode(',', $webrequest['delid']);
			$db->Update('brand_info', "status='-1'", 'id IN ('.$db->BuildInSql($webrequest['delid']).')');
		}
	}
	
	if ($webrequest['access'] == 'updatedown') {
		if (!empty($webrequest['delid'])) {
			$tempdelstr = implode(',', $webrequest['delid']);
			$db->Update('brand_info', "status='0'", 'id IN ('.$db->BuildInSql($webrequest['delid']).')');
		}
	}

	$templogmsg = '';
	if (!empty($tempauditstr))
	{
		$templogmsg .= (empty($templogmsg) ? '' : '; ')."audit: {$tempauditstr}";
	}
	if (!empty($tempdelstr))
	{
		$templogmsg .= (empty($templogmsg) ? '' : '; ')."del: {$tempdelstr}";
	}
	if (empty($templogmsg))
	{
		admin_ShowMessage("没有任何条目被修改，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=brand&{$webrequest['gourl']}");
	}

	admin_AddLog("批量更改品牌|{$templogmsg}");
	admin_ShowMessage('批量更改品牌成功。', ADMIN_WEBURLPATH."/index.php?mode=brand&{$webrequest['gourl']}");
}

template_Display('brand', 1);